/**
 * Created by shidaiapp on 2017/9/29.
 */


function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalDeliver.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalDeliver.isLoading = true;
  });
}
/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 40;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.searchParams.orderSerialNumber = '';
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOrder.getOrderShortageRefund({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "orderSerialNumber":tiyData.tParams.searchParams.orderSerialNumber,
    "state":tiyData.tParams.searchParams.state,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.shortageRefundList.length = 0;
      let shortageRefundPage = response.result;
      tiyData.tPage.total = shortageRefundPage.total;
      tiyData.tPage.stepNumber = shortageRefundPage.pageSize;
      shortageRefundPage.list.forEach(
        function (item) {
          tiyData.tData.shortageRefundList.push(item);
        }
      );
      if (0 == tiyData.tData.shortageRefundList.length) {
        tiyData.tData.shortageRefundList.splice(0, tiyData.tData.shortageRefundList.length);
      }
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 缺货退款的详情信息
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const shortageRefundDetail = (self, tiyData, index) => {
  let selectedRefund = tiyData.tData.shortageRefundList[index];
  tiyData.tData.shortageRefund.id = selectedRefund.id;
  tiyData.tData.shortageRefund.serialNumber = selectedRefund.serialNumber;
  tiyData.tData.shortageRefund.refundPayment = selectedRefund.refundPayment;
  tiyData.tData.shortageRefund.productRefund = selectedRefund.productRefund;
  tiyData.tData.shortageRefund.postageRefund = selectedRefund.postageRefund;
  tiyData.tData.shortageRefund.itemCount = selectedRefund.itemCount;
  tiyData.tData.shortageRefund.productCount = selectedRefund.productCount;
  tiyData.tData.shortageRefund.state = selectedRefund.state;
  tiyData.tData.shortageRefund.type = selectedRefund.type;
  tiyData.tData.shortageRefund.note = selectedRefund.note;
  self.$Spin.show();
  self.$apiOrder.shortageRefundDetail({
    "shortageRefundId": selectedRefund.id,
    "orderId": selectedRefund.orderId
  }).then(function (response) {
    self.$Spin.hide();
    tiyData.tData.shortageRefundDetail.orderItems.splice(0, tiyData.tData.shortageRefundDetail.orderItems.length);
    if (response.success) {
      tiyData.tData.shortageRefundDetail.order = response.result.order;
      let refund = response.result.shortageRefund;
      tiyData.tData.shortageRefund.refundPayment = refund.refundPayment;
      tiyData.tData.shortageRefund.postageRefund = refund.postageRefund;
      tiyData.tData.shortageRefund.type = refund.type;
      tiyData.tData.shortageRefund.note = refund.note;
      tiyData.tData.shortageRefundDetail.order.checkoutTimeStr = self.$tiyBJTime(response.result.order.checkoutTime);
      response.result.orderItems.forEach(
        function (item) {
          tiyData.tData.shortageRefundDetail.orderItems.push(item);
        }
      );
      tiyData.tStatus.modalChangeState.isShow = true;
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 更新缺货退款状态
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webChangeRefundState = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOrder.changeRefundState({
    "id": tiyData.tParams.changeStateParams.refundId,
    "serialNumber": tiyData.tParams.changeStateParams.serialNumber,
    "state":tiyData.tParams.changeStateParams.state,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      self.$Message.success('处理成功!');
      tiyData.tStatus.modalChangeState.isShow = false;
      webList(self,tiyData);
    } else {
      self.$Message.error("处理缺货失败！"+ response.errorMessage);
    }
  });
};

/**
 * 后台计算返退运费
 * @param self
 * @param tiyData
 * @param index
 */
export const countRefundPostage = (self, tiyData, index) => {
  self.$Spin.show();
  let selectedRefund = tiyData.tData.shortageRefundList[index];
  let param = {
    id : tiyData.tData.shortageRefund.id,
    orderId: tiyData.tData.shortageRefundDetail.order.id,
    serialNumber : tiyData.tData.shortageRefund.serialNumber
  };
  let itemIds = [];
  if (tiyData.tData.shortageRefundDetail.orderItems.length > 0) {
    tiyData.tData.shortageRefundDetail.orderItems.forEach(function (item) {
      itemIds.push(item.id);
    });
  }
  param.itemIds = itemIds;
  self.$apiOrder.countRefundPostage(param).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tStatus.modalRefundPostage.isShow = true;
      tiyData.tData.refundPostage = response.result;
    } else {
      let error = "计算返退运费失败！";
      if(response.errorMessage){
        error += response.errorMessage;
      }
      self.$Message.error(error);
    }
  });
};

/**
 * 保存返退运费
 * @param self
 * @param tiyData
 */
export const saveRefundPostage = (self, tiyData, index) => {
  self.$Spin.show();
  let param = {
    id : tiyData.tData.shortageRefund.id,
    refundPostage: tiyData.tData.refundPostage,
    serialNumber : tiyData.tData.shortageRefund.serialNumber
  };
  self.$apiOrder.saveRefundPostage(param).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      self.$Message.success('保存返退运费成功!');
      let shortageRefund = tiyData.tData.shortageRefund;
      shortageRefund.postageRefund = tiyData.tData.refundPostage;
      shortageRefund.refundPayment = Math.floor( shortageRefund.productRefund * 100 + shortageRefund.postageRefund* 100 ) / 100;
      webList(self,tiyData);
    } else {
      let error = "保存返退运费失败！";
      if(response.errorMessage){
        error += response.errorMessage;
      }
      self.$Message.error(error);
    }
  });
};
